﻿using System;
using System.Collections;

namespace YArchitech.HCLoad.XmlData
{
	public class BuildingCollect : Hashtable
	{
		public BuildingCollect(Project project)
		{
			this.parent = project;
			this.AddBuilding();
		}

		public Project Parent
		{
			get
			{
				return this.parent;
			}
			set
			{
				this.parent = value;
			}
		}

		private void AddBuilding()
		{
			try
			{
				Building building = new Building(this);
				int num = this.parent.Parent.AddCurrID();
				building.ID = num;
				this.Add(num, building);
				this.parent.Parent.hCurrIDList.Add(num, building);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}

		public int AddBuilding(Building building)
		{
			int result;
			try
			{
				building.Parent = this;
				int num = this.parent.Parent.AddCurrID();
				building.ID = num;
				this.Add(num, building);
				this.parent.Parent.hCurrIDList.Add(num, building);
				result = num;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return result;
		}

		public void RemoveBuilding(int ID)
		{
			try
			{
				this.Remove(ID);
				this.parent.Parent.hCurrIDList.Remove(ID);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}

		public void ClearBuilding()
		{
			try
			{
				foreach (object obj in this)
				{
					DictionaryEntry dictionaryEntry = (DictionaryEntry)obj;
					this.parent.Parent.hCurrIDList.Remove(dictionaryEntry.Key);
				}
				this.Clear();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}

		public Building GetBuilding(int ID)
		{
			Building result;
			try
			{
				result = (Building)this[ID];
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return result;
		}

		public bool LoadBuilding(Building building)
		{
			bool result;
			try
			{
				building.Parent = this;
				int id = building.ID;
				if (this.parent.Parent.hCurrIDList.ContainsKey(id))
				{
					result = false;
				}
				else
				{
					if (id > this.parent.Parent.CurrID)
					{
						this.parent.Parent.CurrID = id;
					}
					this.Add(id, building);
					this.parent.Parent.hCurrIDList.Add(id, building);
					result = true;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return result;
		}

		private Project parent;
	}
}
